function MyWebSocket(webSocketId, onmessage, onopen, onclose) {
    this.webSocketBaseUrl = process.env.VUE_APP_MESSAGE_SERVER + "/websocket?webSocketId="
    this.webSocketId = webSocketId
    this.socketUrl = this.webSocketBaseUrl + this.webSocketId;
    this.socket = initWebSocket(this.socketUrl)
    this.socket.onmessage = function (event) {
        console.log("收到websocket消息", event.data)
        onmessage(event);
    }
    this.socket.onopen = function (event) {
        console.log("webSocket已连接websocketId=", webSocketId);
        onopen(event);
    }
    this.socket.onclose = function (event) {
        console.log("webSocket已断开websocketId=", webSocketId);
        onclose(event);
    }
    this.close = function () {
        this.socket.close();
    }
}


function initWebSocket(socketUrl) {

    let socket;

    if (!window.WebSocket) {
        window.WebSocket = window.MozWebSocket;
    }

    if (window.WebSocket) {
        socket = new WebSocket(socketUrl);
    }

    return socket
}


export default function webSocketHandler(webSocketId, onmessage, onopen, onclose) {
    return new MyWebSocket(webSocketId, onmessage, onopen, onclose);
}